import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// 白名单，不需要token就可以访问的页面
const whiteList = ['/login', '/404'] // no redirect whitelist

// 前置钩子
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // console.log(from.path, to.path)
  const token = store.getters.token
  // console.log(token)
  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
      // NProgress.done()
    } else {
      const hasGetUserInfo = store.getters.name
      // console.log(hasGetUserInfo)
      if (hasGetUserInfo) {
        next()
      } else {
        await store.dispatch('user/getUserInfo')
        next()
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      // NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
